package com.icebartech.base.modules.common.api;

import com.icebartech.base.modules.common.dto.DictDTO;
import com.icebartech.base.modules.common.service.SysCodeService;
import com.icebartech.core.annotations.RequireLogin;
import com.icebartech.core.components.AliyunOSSComponent;
import com.icebartech.core.components.RedisComponent;
import com.icebartech.core.components.TencentCOSComponent;
import com.icebartech.core.constants.IcebartechConstants;
import com.icebartech.core.constants.UserEnum;
import com.icebartech.core.controller.BaseController;
import com.icebartech.core.vo.BaseData;
import com.icebartech.core.vo.RespJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 系统模块接口controller类
 * @author wenhsh
 *
 */
@Api(description = "系统模块接口")
@RestController
@Slf4j
@RequestMapping(value = {"/base/sys/admin", "/base/sys/app", "/base/sys/mini", "/base/sys/h5"}, produces = "application/json;charset=UTF-8")
public class BaseSysController extends BaseController {
    
    @Autowired
    private SysCodeService sysCodeService;
    
    @Autowired
    private AliyunOSSComponent aliyunOSSComponent;
    
    @Autowired
    private TencentCOSComponent tencentCOSComponent;
    
    @Autowired
    private RedisComponent redisComponent;
    
    
    @ApiOperation("获取系统字典")
    @RequireLogin(userEnum = {UserEnum.admin, UserEnum.app, UserEnum.weixin, UserEnum.h5_master, UserEnum.h5_slaver})
    @PostMapping(value = "/loadDictionary")
    public RespJson<BaseData<List<DictDTO>>> loadDictionary(@ApiParam("字典类型") @RequestParam(value = "codeType") String codeType) {
        return getSuccessRtnMap("获取字典成功", new BaseData<>(sysCodeService.findDictByType(codeType)));
    }
    
    @ApiOperation("获取阿里云OSS文件上传链接")
    @RequireLogin(userEnum = {UserEnum.admin, UserEnum.app, UserEnum.weixin, UserEnum.h5_master, UserEnum.h5_slaver})
    @PostMapping(value = "/getOSSUploadUrl")
    public RespJson<BaseData<Map<String, String>>> getOSSUploadUrl(@ApiParam("文件后缀") @RequestParam(value = "suffix") String suffix, @ApiParam("文件类型") @RequestParam(value = "contentType") String contentType) {
        return getSuccessRtnMap("获取成功", new BaseData<>(aliyunOSSComponent.generateUploadUrl(suffix, contentType)));
    }
    
    @ApiOperation("获取腾讯云COS文件上传链接")
    @RequireLogin(userEnum = {UserEnum.admin, UserEnum.app, UserEnum.weixin, UserEnum.h5_master, UserEnum.h5_slaver})
    @PostMapping(value = "/getCOSUploadUrl")
    public RespJson<BaseData<Map<String, String>>> getCOSUploadUrl(@ApiParam("文件后缀") @RequestParam(value = "suffix") String suffix) {
        return getSuccessRtnMap("获取成功", new BaseData<>(tencentCOSComponent.generateUploadUrl(suffix)));
    }
    
    
    @ApiIgnore
    @PostMapping(value = "/setDisable")
    public RespJson<BaseData<String>> setDisable(@ApiParam("文件后缀") @RequestParam(value = "disable") String disable) {
        RespJson respJson = getFailRtnMap("发生未知错误，请稍后重试！");
        redisComponent.putRedisCache(IcebartechConstants.SYSTEM_DISABLE, IcebartechConstants.SYSTEM_DISABLE, disable);
        return getSuccessRtnMap("获取成功", new BaseData<>(disable));
    }
    
}
